package com.ocl.www.controller;

import com.ocl.www.domain.Comment;
import com.ocl.www.domain.User;
import com.ocl.www.service.ArticleService;
import com.ocl.www.service.CommentService;
import com.ocl.www.service.UserService;
import com.sun.media.jfxmedia.Media;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.ocl.www.basic.BasicController;

import java.sql.Timestamp;
import java.util.*;

@RestController
@RequestMapping("/comment")
public class CommentController extends BasicController {

    @Autowired
    private CommentService commtentService;
    private Logger log = Logger.getLogger(CommentController.class);

    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;


    @ResponseBody
    @RequestMapping(value = "/insertComment",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value="添加评论", notes="添加用户的评论")
    @ApiImplicitParam(name = "maptemp", value = "使用此格式：{}", required = true, dataType = "Map")
    public Map<String,Object> insertComment(@RequestBody Map<String,Object> maptemp){

        log.info("secu : "+ SecurityUtils.getSubject().getPrincipals().asList());
        Set<?> s = SecurityUtils.getSubject().getPrincipals().asSet();
        Object[] obj = new Object[2];
        int obji = 0;
        Iterator<?> it = s.iterator();
        while(it.hasNext()){
            obj[obji] = it.next();
            log.info(obj.getClass());
            log.info(obj.toString());
            obji++;
        }
        Map<String,String> map = (Map<String, String>) obj[1];
        String userId = map.get("userId").toString();

        User user = new User();
        user.setUserId(Integer.valueOf(userId));
        try {
            user = userService.findUserInfo(user);

        } catch (Exception e) {
            e.printStackTrace();
        }

        int commCount = (int) maptemp.get("commCount");

        Comment comm=new Comment();
        Date d = new Date();
        comm.setCreateTime(new Timestamp(d.getTime()));
        comm.setUserId(Integer.valueOf(userId));
        comm.setCommUserName(user.getNickName());
        comm.setArticleId(Integer.valueOf(maptemp.get("articleId").toString()));
        comm.setCommentContent(maptemp.get("comment").toString());
        int c =0 ;
        List<Comment> commentList = null;
        try {
            c = commtentService.insertComment(comm);

            int cflag = articleService.addOneCommCount(comm);
            commentList = commtentService.selectComments(comm,commCount);
        } catch (Exception e) {
            e.printStackTrace();
        }

        maptemp.put("c",c);
        maptemp.put("commentList",commentList);

        return maptemp;


    }




    @ResponseBody
    @RequestMapping(value = "/selectComments",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value="查询文章的评论", notes="根据文章id查询文章的评论")
    @ApiImplicitParam(name = "maptemp", value = "使用此格式：{}", required = true, dataType = "Map")
    public Map<String,Object> selectComments(@RequestBody Map<String,Object> maptemp){
//
//        log.info("secu : "+ SecurityUtils.getSubject().getPrincipals().asList());
//        Set<?> s = SecurityUtils.getSubject().getPrincipals().asSet();
//        Object[] obj = new Object[2];
//        int obji = 0;
//        Iterator<?> it = s.iterator();
//        while(it.hasNext()){
//            obj[obji] = it.next();
//            log.info(obj.getClass());
//            log.info(obj.toString());
//            obji++;
//        }
//        Map<String,String> map = (Map<String, String>) obj[1];
//        String userId = map.get("userId").toString();

        int commCount = (int) maptemp.get("commCount");

        Comment comm=new Comment();
        comm.setArticleId(Integer.valueOf(maptemp.get("articleId").toString()));

        List<Comment> commentList = new ArrayList<Comment>();


        try {
            commentList = commtentService.selectComments(comm,commCount);
        } catch (Exception e) {
            e.printStackTrace();
        }

        maptemp.put("commentList",commentList);
        return maptemp;


    }
}
